package com.technology.education.ui.student.course_detail

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import cn.jzvd.JZMediaInterface
import cn.jzvd.Jzvd
import cn.jzvd.JzvdStd
import com.angcyo.dsladapter.*
import com.technology.education.R
import com.technology.education.data.model.CourseVideo
import com.technology.education.databinding.ActivityCourseDetailBinding
import com.technology.education.dslitem.AppStatusItem
import com.technology.education.ext.loadImage
import com.technology.education.ext.toast
import com.technology.education.utils.JZMediaAliyun
import com.technology.education.widget.CommonButton
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.util.get


/**
 *@author:create by BZY
 *Date: 2022/3/11 11
 * 需要传入 课程id 标题 title 播放连接 url
 */
class CourseDetailActivity :
    BaseVmDbActivity<CourseDetailViewModel, ActivityCourseDetailBinding>() {

    /**提供基本的分割线功能*/
    var baseDslItemDecoration = DslItemDecoration()

    var myAdapter: DslAdapter = DslAdapter().apply {
        dslAdapterStatusItem = AppStatusItem()
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        intent.apply {
            mViewModel.id = get<String>("id") ?: ""
            mViewModel.title = get<String>("title") ?: ""
            mViewModel.url = get<String>("url") ?: ""
        }
        if (mViewModel.url.isNotEmpty()) {
            playVideo()
        }
        mDatabind.jzVideo.watchBack = {
            onBackPressed()
        }
        initRv()
        myAdapter.updateAdapterStatus(DslAdapterStatusItem.ADAPTER_STATUS_LOADING)
        mViewModel.getData()
        mViewModel.getDetail()
    }

    private fun playVideo() {
        mDatabind.jzVideo.setUp(
            mViewModel.url,
            mViewModel.title,
            JzvdStd.SCREEN_NORMAL,
            JZMediaAliyun::class.java
        )

        mDatabind.jzVideo.startVideoAfterPreloading()
    }

    private fun initRv() {
        mDatabind.rvList.apply {
            addItemDecoration(baseDslItemDecoration)
            adapter = myAdapter
            layoutManager = LinearLayoutManager(context)
        }
    }

    override fun onBackPressed() {
        if (Jzvd.backPress()) {
            return
        }
        super.onBackPressed()
    }

    @SuppressLint("SetTextI18n")
    private fun setUI(courseVideoList: List<CourseVideo>) {
        myAdapter.render {
            resetItem(courseVideoList.toDslItemList(R.layout.item_course_catalog) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemData?.let {
                        if (it is CourseVideo) {
                            itemTag = it.id
                            itemHolder.tv(R.id.tv_title)?.apply {
                                text = it.name
                                setTextColor(getColor(if (mViewModel.currentId == it.id) R.color.theme_color else R.color.text_h))
                            }
                            itemHolder.tv(R.id.tv_num)?.text = "${it.playNumber}人观看"
                            itemHolder.v<CommonButton>(R.id.btn_learn)?.apply {
                                text = "学习"
                                visibility =
                                    if (mViewModel.currentId == it.id) View.INVISIBLE else View.VISIBLE
                            }
                            itemHolder.v<ImageView>(R.id.iv_cover)?.apply {
                                loadImage(it.image, this)
                            }
                            itemHolder.click(R.id.btn_learn) { _ ->
                                mViewModel.url = it.url
                                mViewModel.title = it.name
                                mViewModel.currentId = it.id
                                playVideo()
                            }
                        }
                    }

                }
            })
        }
    }

    override fun onPause() {
        super.onPause()
        Jzvd.releaseAllVideos()
    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

    override fun createObserver() {
        mViewModel.detail.observe(this){
            parseState(it, onSuccess = {detail->
                myAdapter.setAdapterStatus(DslAdapterStatusItem.ADAPTER_STATUS_NONE)
                setUI(detail.courseVideoList)
            }, onError = {
                myAdapter.updateAdapterStatus(DslAdapterStatusItem.ADAPTER_STATUS_ERROR)
            })
        }
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { detail ->
                mViewModel.currentId=detail.id
                mViewModel.title=detail.name
                mViewModel.url=detail.url
                mViewModel.hasCollected.set(detail.collected=="1")
                playVideo()
            }, onError = {
               toast(it.errorMsg)
            })
        }
    }
}